package com.lfz.task.service;


import com.lfz.task.config.MoblieMessageProperties;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class MoblieMessageService {

    @Autowired
    private MoblieMessageProperties properties;

    private String url;
    private String appKey;
    private String secret;
    private String smsType;
    private String smsFreeSignname;
    private String smsTemplateCode;

    @PostConstruct
    public void init(){
        this.url = properties.getUrl();
        this.appKey = properties.getAppKey();
        this.secret = properties.getSecret();
        this.smsType = properties.getSmsType();
        this.smsFreeSignname = properties.getSmsFreeSignname();
        this.smsTemplateCode = properties.getSmsTemplateCode();
    }

    /**
     * @param recNum 接收短信手机号，必须是字符串形式
     * @param code
     * @throws ApiException
     */
    public void sendSms(String recNum, String code) throws ApiException {

        String smsParamString = "{code:'" + code + "'}";

        TaobaoClient client = new DefaultTaobaoClient(url, appKey, secret);
        AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        req.setExtend("");
        req.setSmsType(smsType);
        req.setSmsFreeSignName(smsFreeSignname);
        req.setSmsParamString(smsParamString);
        req.setRecNum(recNum);
        req.setSmsTemplateCode(smsTemplateCode);
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
    }

}
